﻿using God.ShoppingWebApi.DTO;
using God.ShoppingWebApi.Entities;
using God.ShoppingWebApi.IService;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace God.ShoppingWebApi.Service
{
    /// <summary>
/// 角色
/// </summary>
    public class AdminRoleService : ApplicationService, IAdminRoleService
    {
        IRepository<Roles, Guid> _roles;
        public AdminRoleService(IRepository<Roles, Guid> roles)
        {
            _roles = roles;
        }

        [Route("api/RoleAdd")]
        /// <summary>
        /// 角色添加
        /// </summary>
        /// <param name="roleDTO"></param>
        /// <returns></returns>
        public async Task AddAsync(RoleDTO roleDTO)
        {
            Roles ci = new Roles(GuidGenerator.Create(),
                roleDTO.RoleName,
              roleDTO.Status,
              roleDTO.Create_time,
              roleDTO.Update_time,
              roleDTO.Describe,
             
           
              CurrentUser.Id);

            await _roles.InsertAsync(ci);
        }
        [Route("api/RoleDel")]
        /// <summary>
        /// 角色删除
        /// </summary>
        /// <param name="gu"></param>
        /// <returns></returns>
        public async Task DeleteAsync(Guid gu)
        {
            await _roles.DeleteAsync(gu);
        }
        [HttpGet]
        [Route("api/RoleEdit")]
        /// <summary>
        /// 角色反填
        /// </summary>
        /// <param name="gid"></param>
        /// <returns></returns>
        public async Task<RoleDTO> Edit(Guid gid)
        {
            var data = await _roles.GetAsync(gid);
            var RoleDTO = ObjectMapper.Map<Roles, RoleDTO>(data);
            return RoleDTO;
        }
        [HttpGet]
        [Route("api/RoleShow")]
        /// <summary>
        /// 角色显示
        /// </summary>
        /// <returns></returns>
        public async Task<List<RoleDTO>> GetListAsync()
        {
            var str = await _roles.GetListAsync();

            return ObjectMapper.Map<List<Roles>, List<RoleDTO>>(str);
        }
        [Route("api/RoleUpdate")]
        /// <summary>
        /// 角色修改
        /// </summary>
        /// <param name="roleDTO"></param>
        /// <returns></returns>
        public async Task<RoleDTO> Update(RoleDTO roleDTO)
        {

            var data = await _roles.GetAsync(roleDTO.Id);
            if (data != null)
            {
                data.LastModificationTime = DateTime.Now;
                data.RoleName = roleDTO.RoleName;
                data.Status = roleDTO.Status;
                data.Create_time = roleDTO.Create_time;
                data.Update_time = roleDTO.Update_time;
                data.Describe = roleDTO.Describe;
              
                var Info = await _roles.UpdateAsync(data);

                return roleDTO;
            }
            else
            {
                return null;
            }
        }
    }
}
